import {
  Component,
  OnInit,
  Injector,
  ViewEncapsulation,
  ViewChild
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import {
  MaterialIntoServiceProxy,
  WlReleaseStockBillListDto,
  MaterialImportIntoListDto
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent, Paginator } from 'primeng/primeng';
import { finalize } from 'rxjs/operators';
import { DecimalPipe } from '@angular/common';
import * as moment from 'moment';
@Component({
  selector: 'app-into',
  templateUrl: './into.component.html',
  styleUrls: ['./into.component.less'],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})
export class MaterialIntoComponent extends AppComponentBase implements OnInit {
  @ViewChild('paginator') paginator: Paginator;

  filterText = '';

  hgzNo = '';

  bills = new Array<WlReleaseStockBillListDto>();

  constructor(
    injector: Injector,
    private _warehouseIntoServiceProxy: MaterialIntoServiceProxy
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.getBills();
  }

  scanBillByHgzNo() {
    this._warehouseIntoServiceProxy
      .scanMaterialByHgz(this.hgzNo)
      .pipe(finalize(() => (this.hgzNo = '')))
      .subscribe(result => {
        this.getBills();
      });
  }

  getBills(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._warehouseIntoServiceProxy
      .getReleaseStockBills(
        this.filterText,
        undefined,
        12,
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.bills = result.items;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  deleteWl(wl: WlReleaseStockBillListDto): void {
    this.message.confirm(
      '确认删除' + wl.materialName + '吗？',
      this.l('AreYouSure'),
      isConfirmed => {
        if (isConfirmed) {
          this._warehouseIntoServiceProxy
            .deleteReleaseStockBill(wl.id)
            .subscribe(() => {
              this.getBills();
              this.notify.success(this.l('SuccessfullyDeleted'));
            });
        }
      }
    );
  }
  onUpload(event) {
    let list = event as Array<any>;

    let materialImportIntoList = list.map(el => {
      return {
        taskCode: el.任务号,
        drawingCode: el.图号,
        materialName: el.物资名称,
        invFurBatch: el.炉批号,
        hgzNo: el.合格证号,
        hgz_zjd: el.化验单号,
        hgz_zyd: el.质疑单号,
        zhuanYongStatus: el.专料专用,
        invCode: el.物资编码,
        invXhPhCpdh: el.牌号,
        techCondition: el.技术状态,
        invScale: el.规格,
        invStatus: el.物资状态,
        invMeasName: el.计量单位,
        invDepMeasName: el.副计量单位,
        weight: Number(el.重量),
        quantity: Number(el.数量),
        unitPrice: Number(el.单价),
        jcCc: el.尺寸,
        materialType: el.材料类型,
        materialLY: el.材料来源,
        remark: el.备注,
        areaName: el.区域,
        shelfName: el.货架,
        positionName: el.货位
      };
    }) as Array<MaterialImportIntoListDto>;

    this.message.confirm(
      '确认导入这' + list.length + '条数据吗？',
      this.l('AreYouSure'),
      isConfirmed => {
        if (isConfirmed) {
          this.primengTableHelper.showLoadingIndicator();
          this._warehouseIntoServiceProxy
            .importIntoWarehouse(materialImportIntoList)
            .subscribe(result => {
              this.reloadPage();
              this.notify.success('导入数据成功');
            });
        }
      }
    );
  }
  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }
}
